/**
 * @description tabsBar标签页逻辑，如无必要请勿修改
 */
const state = () => ({
    visitedRoutes: [],
})
const getters = {
    visitedRoutes: (routeState) => routeState.visitedRoutes,
}
const mutations = {
    /**
     * @description 添加标签页
     * @param {*} routeState
     * @param {*} route
     * @returns
     */
    addVisitedRoute(routeState, route) {
        const target = routeState.visitedRoutes.find((item) => item.path === route.path);
        if (target && !route.meta.dynamicNewTab) {
            Object.assign(target, route);
        } else if (!target) {
            routeState.visitedRoutes.push(Object.assign({}, route));
        }
    },
    /**
     * @description 删除当前标签页
     * @param {*} routeState
     * @param {*} path
     * @returns
     */
    delVisitedRoute(routeState, path) {
        routeState.visitedRoutes.forEach((item, index) => {
            if (item.path === path) {
                routeState.visitedRoutes.splice(index, 1);
            }
        })
    },
    /**
     * @description 删除当前标签页以外其它全部标签页
     * @param {*} routeState
     * @param {*} path
     * @returns
     */
    delOthersVisitedRoutes(routeState, path) {
        routeState.visitedRoutes = routeState.visitedRoutes.filter(item => item.meta.noClosable || item.path === path);
    },
    /**
     * @description 删除当前标签页左边全部标签页
     * @param {*} routeState
     * @param {*} path
     * @returns
     */
    delLeftVisitedRoutes(routeState, path) {
        const idx = routeState.visitedRoutes.findIndex((item) => item.path === path);
        routeState.visitedRoutes = routeState.visitedRoutes.filter((item, index) => {
            return item.meta.noClosable || index >= idx
        })
    },
    /**
     * @description 删除当前标签页右边全部标签页
     * @param {*} routeState
     * @param {*} path
     * @returns
     */
    delRightVisitedRoutes(routeState, path) {
        const idx = routeState.visitedRoutes.findIndex((item) => item.path === path);
        routeState.visitedRoutes = routeState.visitedRoutes.filter((item, index) => {
            return item.meta.noClosable || index <= idx
        })
    },
    /**
     * @description 删除全部标签页
     * @param {*} routeState
     * @returns
     */
    delAllVisitedRoutes(routeState) {
        routeState.visitedRoutes = routeState.visitedRoutes.filter(item => item.meta.noClosable);
    },
    /**
     * @description 修改标题
     * @param {*} routeState
     * @param options
     */
    changeTabsMeta(routeState, options) {
        function handleVisitedRoutes(visitedRoutes) {
            return visitedRoutes.map((route) => {
                if (route.name === options.name || route.meta.title === options.title) {
                    Object.assign(route.meta, options.meta);
                }
                if (route.children && route.children.length) {
                    route.children = handleVisitedRoutes(route.children);
                }
                return route;
            })
        }

        routeState.visitedRoutes = handleVisitedRoutes(routeState.visitedRoutes)
    },
}
const actions = {
    /**
     * @description 添加标签页
     * @param {*} { commit }
     * @param {*} route
     */
    addVisitedRoute({commit}, route) {
        commit('addVisitedRoute', route);
    },
    /**
     * @description 删除当前标签页
     * @param {*} { commit }
     * @param {*} path
     */
    delVisitedRoute({commit}, path) {
        commit('delVisitedRoute', path);
    },
    /**
     * @description 删除当前标签页以外其它全部标签页
     * @param {*} { commit }
     * @param {*} path
     */
    delOthersVisitedRoutes({commit}, path) {
        commit('delOthersVisitedRoutes', path);
    },
    /**
     * @description 删除当前标签页左边全部标签页
     * @param {*} { commit }
     * @param {*} path
     */
    delLeftVisitedRoutes({commit}, path) {
        commit('delLeftVisitedRoutes', path);
    },
    /**
     * @description 删除当前标签页右边全部标签页
     * @param {*} { commit }
     * @param {*} path
     */
    delRightVisitedRoutes({commit}, path) {
        commit('delRightVisitedRoutes', path);
    },
    /**
     * @description 删除全部标签页
     * @param {*} { commit }
     */
    delAllVisitedRoutes({commit}) {
        commit('delAllVisitedRoutes');
    },
    /**
     * @description 修改Route Meta
     * @param {*} { commit }
     * @param options
     */
    changeTabsMeta({commit}, options = {}) {
        commit('changeTabsMeta', options);
    },
}
export default {state, getters, mutations, actions}
